package completion_failure;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;


@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedAnnotationTypes("java.lang.SuppressWarnings")
public class AnnotationProcessor extends AbstractProcessor
{
	private Set<Element> elements = new HashSet<>();

	public Set<Element> getElements()
	{
		return elements;
	}

	private void populateElements(Element element, int depth)
	{
		if (elements.add(element))
		{
			TypeMirror mirror = element.asType();

			System.out.println(
					new String(new char[depth]).replace("\0", " ") +
					mirror.toString() + " " + element.getSimpleName());

			for (Element enclosedElement : element.getEnclosedElements())
			{
				populateElements(enclosedElement, depth + 1);
			}
			
			if (mirror instanceof DeclaredType)
			{
				DeclaredType declaredType = (DeclaredType) mirror;
				populateElements(declaredType.asElement(), depth + 1);
			}
		}
	}

	@Override
  public boolean process(
  		Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
  {
		System.out.println("starting processing round");

		if (false == roundEnv.processingOver())
		{
			populateElements(roundEnv.getRootElements().iterator().next(), 0);
		}

		return false;
  }
}